package com.yoso.base.interfaces.controller;


import com.github.pagehelper.PageInfo;
import com.yoso.base.entity.BaseEntity;
import com.yoso.base.interfaces.vo.BaseVo;
import com.yoso.base.interfaces.vo.PageVo;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestParam;

import java.util.List;

/**
 * @author shizejin
 * @date 2021/11/9
 * @description:
 */
public abstract class BaseController<V extends BaseVo, E extends BaseEntity> extends AbstractBaseController<V, E> {

    /**
     * 条件分页查询
     */
    @ApiOperation("分页条件查询")
    @PostMapping("/pageConditionsList")
    public BaseResponse<PageInfo<V>> pageConditionsList(@RequestBody PageVo pageVo) {
        PageInfo<V> result = this.getService().pageConditionsList(pageVo);
        return this.success(result);
    }

    @ApiOperation("新增/修改")
    @PostMapping("/saveOrUpdate")
    public BaseResponse saveOrUpdate(@RequestBody V vo) {
        int result = this.getService().insert(vo);
        return this.result(result);
    }

    @ApiOperation("查询全部")
    @GetMapping("/listAll")
    public BaseResponse<List<V>> listAll() {
        return this.success(this.getService().listAll());
    }

    @ApiOperation("根据ID删除")
    @PostMapping("/deleteByIds")
    public BaseResponse deleteByIds(@RequestBody Iterable<String> ids) {
        ids.forEach(id -> this.getService().deleteByPrimaryKey(id));
        return this.success();
    }

    @ApiOperation("根据ID查询")
    @PostMapping("/findById")
    public BaseResponse<V> findById(@RequestParam("id") String id) {
        return this.success(this.getService().selectByPrimaryKey(id));
    }

    @ApiOperation("根据id修改")
    @PostMapping("/updateById")
    public BaseResponse updateById(@RequestBody V vo) {
        return this.success(this.getService().updateByPrimaryKey(vo));
    }
}
